package com.jh.user.repository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jh.user.pojo.UserRealtion;

@Repository
public interface UserRelationRepository extends JpaRepository<UserRealtion, Long>,JpaSpecificationExecutor<UserRealtion>{

	List<UserRealtion> findByFirstUserIdOrderByLevel(Long userId);

	UserRealtion findByFirstUserIdAndPreUserId(long userId, long preUserId);

	List<UserRealtion> findByPreUserIdOrderByLevel(long preUserId);

	List<UserRealtion> findByPreUserIdAndLevelInOrderByLevel(long userId, List<Integer> level);

	@Query("select ur.firstUserId from UserRealtion ur where ur.preUserPhone=:preUserPhone and ur.createTime>=:startTime and ur.createTime<=:endTime")
	List<Long> findUserAgentChangeByTimeAndPhone(@Param("preUserPhone") String preUserPhone, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
	
	@Query("select ur.firstUserId from UserRealtion ur where ur.preUserPhone=:preUserPhone and ur.level=:level and ur.createTime>=:startTime and ur.createTime<=:endTime")
	List<Long> findUserAgentChangeByTimeAndPhoneAndLevel(@Param("preUserPhone") String preUserPhone, @Param("level") String level, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

	UserRealtion findByFirstUserIdAndFirstUserGradeAndLevel(long preUserId, Integer preUserGrade, int level);

	UserRealtion findByFirstUserIdAndPreUserIdAndFirstUserGrade(Long firstUserId, Long preUserId,Integer firstUserGrade);

}
